<?php
header("Cache-Control: no-cache, must-revalidate");
    $name=$_GET["name"];
    $con = mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("tubetogether", $con);
    $sql="SELECT * FROM messages WHERE Receiver = '$name'";

    $result = mysql_query($sql);
    $arr=array();

    while ($row=mysql_fetch_array($result)) {
        $arr[]=array("MID" => $row['MID'], "Sender" => $row['Sender'], "Message" => $row['Message']);
    }

    if (count($arr)>0) {
        $sql="DELETE FROM messages WHERE MID IN (";
        foreach($arr as $k => $v) {
            $sql=$sql.$arr[$k]['MID'].",";
        }
        $sql=substr($sql, 0, strlen($sql)-1).")";
    }

    //$result = mysql_query($sql);
    mysql_close($con);
    echo "<?xml version=\"1.0\"?>\n";
    echo "<messages>\n";
    foreach($arr as $k => $v) {
        echo "<item>\n";
        echo "<sender>".$arr[$k]['Sender']."</sender>\n";
        echo "<message>".$arr[$k]['Message']."</message>\n";
        echo "</item>\n";
    }
    echo "</messages>\n";
    
?>
